package turing.forum.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.SneakyThrows;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.index.query.TermsQueryBuilder;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import turing.forum.repository.mapper.ShareMapper;
import turing.forum.repository.po.User;
import turing.forum.repository.po.UserShare;
import turing.forum.service.IShareService;

import java.util.ArrayList;
import java.util.List;

@Service
public class ShareService implements IShareService {
    @Autowired
    private ShareMapper shareMapper;
    @Autowired
    @Qualifier(value = "restHighLevelClient")
    private RestHighLevelClient esClient;
    @SneakyThrows
    @Override
    public Page<UserShare> listShares(User user, Integer pageNum, Integer pageSize) {
        QueryWrapper<UserShare> queryWrapper = new QueryWrapper<UserShare>()
                .eq("user_id", user.getId());
        Page<UserShare> sharePage = new Page<>(pageNum, pageSize);
        sharePage = shareMapper.selectPage(sharePage, queryWrapper);
        ArrayList<Long> ids = new ArrayList<>();
        sharePage.getRecords().forEach(share -> ids.add(share.getId()));
        SearchRequest request = new SearchRequest("turing-forum-shares");
        TermsQueryBuilder queryBuilder = QueryBuilders.termsQuery("id", ids);
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        sourceBuilder.query(queryBuilder);
        request.source(sourceBuilder);
        SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);

        return sharePage;
    }

    @Override
    public UserShare deleteShare(UserShare share) {
        return null;
    }

    @Override
    public UserShare queryShare(UserShare share) {
        return null;
    }

    @SneakyThrows
    @Override
    public UserShare addShare(UserShare share) {
        long id = IdWorker.getId();
        share.setId(id);
        int insertShare = shareMapper.insert(share);
        IndexRequest request = new IndexRequest()
                .id(String.valueOf(id))
                .source(JSON.toJSONString(share), XContentType.JSON);
        IndexResponse response = esClient.index(request, RequestOptions.DEFAULT);
        int status = response.status().getStatus();
        return null;
    }
}
